//
//  SubjectTabVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/1.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "SubjectTabVC.h"
#import "MySubjectCell.h"
//#import "SubjectModel.h"
#import "SpecialRecommendationModel.h"
#import "UIImage+ImgSize.h"
#import "SubjectRecommendContentTabVC.h"
#import "XHWebImageAutoSize.h"
@interface SubjectTabVC ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,assign) NSInteger id;
@property(nonatomic,assign) NSInteger uid;
@property(nonatomic,assign) NSInteger rid;
@property(nonatomic,assign) NSInteger did;
@property(nonatomic,assign) NSInteger aid;
@property(nonatomic,assign) NSInteger sid;
@property(nonatomic,strong) NSMutableArray *subjectMutb;
@property(nonatomic,assign) NSInteger pageSize;
@property(nonatomic,assign) NSInteger pageNumber;
@property(nonatomic,strong) NSMutableArray *demandMuta;
@property(nonatomic,strong) NSMutableArray *serviceMuta;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) MJRefreshAutoNormalFooter *footer;

@end
static NSString *identifierSubject = @"MySubjectCell";
@implementation SubjectTabVC
-(NSMutableArray *)subjectMutb
{
    if (!_subjectMutb) {
        _subjectMutb = [NSMutableArray array];
    }
    return _subjectMutb;
}
-(NSMutableArray *)demandMuta
{
    if (!_demandMuta) {
        _demandMuta = [NSMutableArray array];
    }
    return _demandMuta;
}
-(NSMutableArray *)serviceMuta
{
    if (!_serviceMuta) {
        _serviceMuta = [NSMutableArray array];
    }
    return _serviceMuta;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.tableView registerNib:[UINib nibWithNibName:@"MySubjectCell" bundle:nil] forCellReuseIdentifier:identifierSubject];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(collectioncollectionRecommend:) name:@"collectioncollectionRecommend" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiusibai:) name:@"qingqiusibai" object:nil];
    self.manager = [AFHTTPSessionManager manager];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.mj_header = header;
    [header beginRefreshing];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"empty"];
    return image;
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = [NSBundle bundleLocalizedStringForKey:@"noData"];
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
                                 NSForegroundColorAttributeName:kLabel102Color
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

-(void)qingqiusibai:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
    [self.tableView.mj_header endRefreshing];
    [WSProgressHUD dismiss];
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

-(void)reloadInternetData
{
    self.subjectMutb = nil;
    [self.footer removeFromSuperview];
    NSString *pageNums = [NSString stringWithFormat:@"%ld",self.pageNumber];
    NSString *pageSizes = [NSString stringWithFormat:@"%ld",self.pageSize];
    self.dict = @{
                  @"uid":[UserDefaultsHelper readUid],
                  @"pageNumber":pageNums,
                  @"pageSize":pageSizes,
                  @"token":[UserDefaultsHelper readToken]
                  };
    XLog(@"self.dict:%@",self.dict);
    [CustomeManager POSTWithURLIndex:25 parameters:self.dict success:^(NSDictionary *dict) {
        
    } failure:^(NSError *error) {
        
    }];
}
-(void)collectioncollectionRecommend:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    
    NSArray *date = [dict objectForKey:@"date"];
    if (date.count) {
        for (NSDictionary *dict in date) {
            SpecialRecommendationModel *model = [SpecialRecommendationModel mj_objectWithKeyValues:dict];
            [self.subjectMutb addObject:model];
            
        }
        [self.tableView.mj_footer endRefreshing];
    }
    if (self.subjectMutb.count) {
        self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMore)];
        self.tableView.mj_footer = self.footer;
        if (date.count&&date.count<10) {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
            [self.footer setTitle:@"~~~ 我是有底线的 ~~~" forState:MJRefreshStateNoMoreData];
            self.footer.stateLabel.font = [UIFont systemFontOfSize:12];
            self.footer.stateLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
        }else {
            [self.tableView.mj_footer endRefreshing];
        }
    }else {
        self.tableView.emptyDataSetSource = self;
        self.tableView.emptyDataSetDelegate = self;
    }
    [self.tableView reloadData];
    [self.tableView.mj_header endRefreshing];
    [WSProgressHUD dismiss];
}
-(void)refresh
{
    self.subjectMutb = nil;
    self.pageNumber = 1;
    self.pageSize = 10;
    self.uid = 1;
    [self reloadInternetData];
}
-(void)reloadMore
{
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    self.pageNumber ++;
    [self reloadInternetData];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.subjectMutb.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    MySubjectCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierSubject forIndexPath:indexPath];
    SpecialRecommendationModel *model = self.subjectMutb[indexPath.section];
    
    cell.subjectModel = model;
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SpecialRecommendationModel *model = self.subjectMutb[indexPath.section];
    return [XHWebImageAutoSize imageHeightForURL:[NSURL URLWithString:model.icon] layoutWidth:[UIScreen mainScreen].bounds.size.width-18*2 estimateHeight:200];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SpecialRecommendationModel *model = self.subjectMutb[indexPath.section];
    if (self.returnValueBlock) {
        self.returnValueBlock(model.type, model);
    };
    
}


@end
